<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>事件处理</title>
</head>

<body>
    <h2>{{ counter }}</h2>

    <button id="minus">-</button>
    <button id="plus">+</button>
    <script>
        let counter = 0

        // 设置计数器显示初始值
        let counterElement = document.querySelector("h2")
        counterElement.textContent = counter

        let minusElement = document.querySelector("#minus")
        let plusElement = document.querySelector("#plus")

        minusElement.addEventListener("click", function () {
            --counter
            counterElement.textContent = counter
        })

        plusElement.onclick = function () {
            ++counter
            counterElement.textContent = counter
        }

    </script>
</body>

</html>